<?php
namespace app\api\controller;
use app\common\model\CommentOld;

class Comment extends Base
{
    protected $noNeedLogin = ['getCommentsList','getChildInfo'];
    public function my_comment2()
    {
       //获取到我发表的文章
       $uid= $this->auth->id;
//       $art_ids = db('article')->where('user_id',$this->auth->id)->column('id');
       $comment_model = new \app\common\model\CommentOld();
//       $where1=[];
//       $where2=[];
//       if($art_ids){
//           $where1['aid']=['in',$art_ids];
//           $where1['level']=['eq',1];
//       }
//       $where2['to_user_id'] = $uid;
//       $where2['level']=['neq',1];
       $list = $comment_model
        ->field('id')
        ->where('to_user_id',$uid)

//        ->where(function($query)use($where1){
//            $query->where($where1);
//        })
//        ->whereor(function($query)use($where2){
//           $query->where($where2);
//        })
        ->select();
       $this->success('获取成功',$list,200);
    }

    //获取到对我的评论
    public function my_comment()
    {
        //获取到我发表的文章
        $uid= $this->auth->id;
        $comment_model = new \app\common\model\Comment();
        $art_ids = db('article')->where('user_id',$uid)->column('id');
        $whereOr=[];
        if($art_ids){
            $whereOr['c.aid']=['in',$art_ids];
            $whereOr['c.user_id']=['neq',$uid];
        }
        $where=[];
        $where['c.to_user_id']=$uid;
        //查询所有aid
        $list = $comment_model
            ->alias('c')
            ->field('c.id,c.show_reply,c.content,u.nickname,c.createtime,u.avatar,c.aid,c.top_id,c.pid,c.to_user_id,c.user_id,a.user_id author_id')
            ->join('user u','c.user_id = u.id','left')
            ->join('article a','c.aid = a.id','left')
            ->where($where)
            ->whereOr(function ($query)use($whereOr){
                $query->where($whereOr);
            })
            ->order('c.createtime desc')
            ->paginate(10);
        $list = json_decode(json_encode($list),true);
        if($list['data']){
            foreach($list['data'] as &$v){
                $v['is_author']= 0;//不是作者
                if($v['user_id'] == $v['author_id']){
                    $v['is_author']= 1;
                }
                $v['createtime'] = wordTime($v['createtime']);
                $v['is_comment'] = 0;//对文章评论
                $v['parent_content']="该评论已删除";
                if($v['top_id']){
                    $info = db('comment')->where('id',$v['pid'])->field('content,id,pid,top_id')->find();
                    if($info){
                        //对文章的评论
                        $v['is_comment'] =1;//回复
                        $v['parent_content'] = $info['content'];
                    }
                }

            }
        }
        
        $comment_model->where($where)
            ->alias('c')
            ->where('is_read',0)
            ->whereOr(function ($query)use($whereOr,$uid){
                $query->where($whereOr)->whereOr('c.to_user_id',$uid);
            })->update(['is_read'=>1]);
        $this->success('获取成功',$list,200);
    }

    //获取到文章的评论  顶级评论
    public function art_comment()
    {
        $aid = $this->request->param('aid/d');
        if(!$aid){
            $this->error('参数错误');
        }
        $comment_model = new \app\common\model\CommentOld();
        $where['aid'] = $aid;
        $where['level'] = 1;
        $list = $comment_model->where($where)->paginate(10);
        $this->success('获取成功',$list,200);
    }

    public function child()
    {
        //根据顶级评论
        $comment_id = $this->request->param('comment_id/d');//把顶级评论传过来
        $aid = $this->request->param('aid/d');//文章id
        $comment_model = new \app\common\model\CommentOld();
        $where['top_id']=$comment_id;
        $where['aid']=$aid;
//        $comment_model->where($where)->
    }

    public function getCommentsList()
    {
        $aid = $this->request->param('aid/d');//文章id
        $article_model = new \app\common\model\Article();
        $info=$article_model->where('id',$aid)->find();
        if(!$aid || !$info){
            $this->error('参数有误');
        }
        $author_id = $info->user_id;
        $comment_model = new \app\common\model\Comment();
        $limit = 2;
//        $list =  $comment_model
//            ->alias('c')
//            ->field('c.id,c.aid,c.pid,c.top_id,c.user_id,u.nickname,u.avatar,c.createtime,c.content')
//            ->where('aid', '=', $aid)
//            ->where('pid', 0)
//            ->join('user u','c.user_id = u.id','left')
//            ->with(['child' =>function ($query) use ($limit) {
//                $query->alias('cl')->join('user cc','cl.user_id = cc.id','left')->field('cl.id,cl.top_id,cc.nickname,cc.avatar,cl.id,ccc.nickname reply_nickname,cl.user_id,cl.to_user_id,cl.createtime,cl.content,cl.pid,cl.show_reply')->join('user ccc','cl.to_user_id = ccc.id','left')->order('cl.createtime desc')->limit(0, $limit);
//            }])
//            ->order('c.createtime desc')
//            ->paginate(10);

        $list =  $comment_model
            ->alias('c')
            ->field('c.id,c.aid,c.pid,c.top_id,c.user_id,u.nickname,u.avatar,c.createtime,c.content')
            ->where('aid', '=', $aid)
            ->where('pid', 0)
            ->join('user u','c.user_id = u.id','left')
            ->order('c.createtime desc')
            ->paginate(10);
        $list = json_decode(json_encode($list),true);
        if($list['data']){
            foreach ($list['data'] as &$lv){
                    $lv['child'] = [];
                    $child = db('comment')
                    ->alias('cl')
                    ->where('cl.top_id',$lv['id'])
                    ->field('cl.id,cl.top_id,cc.nickname,cc.avatar,cl.id,ccc.nickname reply_nickname,cl.user_id,cl.to_user_id,cl.createtime,cl.content,cl.pid,cl.show_reply')
                    ->join('user cc','cl.user_id = cc.id','left')
                    ->join('user ccc','cl.to_user_id = ccc.id','left')
                    ->order('cl.createtime desc')
                    ->limit($limit)
                    ->select();
                    if($child){
                        $lv['child'] = $child;
                    }
            }
        }
        
        $list['art_total'] = $info->comments;
        if($list['data']){
            foreach ($list['data'] as &$v){
                $is_author = 0;
                if($v['user_id'] == $author_id){
                    $is_author =1;
                }
                $v['is_author'] = $is_author;
//                $v['createtime'] = date('Y-m-d H:i',$v['createtime']);
                $v['createtime'] = wordTime($v['createtime']);
                //这个是前几个的 默认排除
                $now = 0;
                $total = $comment_model->where(['top_id'=>$v['id']])->count();
                if($v['child']){
                    $now = count($v['child']);
                    foreach ($v['child'] as &$vv){
                        $vv['createtime'] = wordTime($vv['createtime']);
                        $vv['is_author'] = 0;
                        if($vv['user_id'] == $author_id){
                            $vv['is_author'] = 1;
                        }
//                        $vv['show_reply'] = 1;//是否显示回复
//                        if($v['id'] == $vv['pid']){
//                            $vv['show_reply'] = 0;
//                        }
                    }
                }
                $v['free_count'] = $total = $total - $now ;
            }
        }
        $this->success('获取成功',$list,200);
    }

    //获取子评论的接口
    public function getChildInfo()
    {
        //获取顶级评论id
        $top_id = $this->request->post('top_id/d');
        $aid = $this->request->post('aid/d');
        //把最开始的展示出来的俩个id 穿过来 数组的形式
        $has_id = $this->request->post('has_id/a',[]);
        if(!$top_id || !$aid || !is_array($has_id)){
            $this->error('参数有误');
        }

        $article_model = new \app\common\model\Article();
        $info=$article_model->where('id',$aid)->find();
        if(!$info){
            $this->error('获取失败');
        }
        $author_id = $info->user_id;

        $where['top_id'] = $top_id;
        if($has_id){
            $where['id']=['not in',$has_id];
        }
        $where['aid'] = $aid;
        $comment_model = new \app\common\model\Comment();
        $list = $comment_model
            ->field("*")
            ->with(['user','toUser'])
            ->where($where)
            ->order('createtime desc')
            ->paginate(10);
        $list = json_decode(json_encode($list),true);
        if($list['data']){
            foreach ($list['data'] as &$v){
//                $v['createtime']=date('Y-m-d H:i',$v['createtime']);
                $v['createtime']=wordTime($v['createtime']);
                $v['is_author'] = 0;
                if($v['user_id'] ==$author_id){
                    $v['is_author'] = 1;
                }
            }
        }
        $this->success('获取成功',$list,200);
    }
}